package fulbiNet.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import fulbiNet.execpcion.DAOExcepcion;
import fulbiNet.modelo.Persona;
import fulbiNet.util.ConexionBD;

public class PersonaDAO extends BaseDAO {

	public void insertar(Persona vo) throws DAOExcepcion {
		System.out.println("PersonaDAO: insertar(Persona vo)");
		String query = "INSERT INTO Persona(nombres,paterno,materno,sexo,tipodocumento,nrodocumento,email,contrasena,fnacimiento,celular) VALUES (?,?,?,?,?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			
			stmt.setString(1, vo.getNombres());
			stmt.setString(2, vo.getPaterno());
			stmt.setString(3, vo.getMaterno());
			stmt.setString(4, vo.getSexo());
			stmt.setString(5, vo.getTipodocumento());
			stmt.setString(6, vo.getNrodocumento());
			stmt.setString(7, vo.getEmail());
			stmt.setString(8, vo.getContrasena());
			stmt.setDate(9, vo.getFnacimiento());
			stmt.setString(10, vo.getCelular());
			
						
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public Persona obtener(int idPersona) throws DAOExcepcion {
		System.out.println("PersonaDAO: obtener(int idPersona)");
		Persona vo = new Persona();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select idPersona, nombres,paterno,materno,sexo,tipodocumento,nrodocumento,email,contrasena,fnacimiento,celular from Persona where idPersona=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idPersona);
			rs = stmt.executeQuery();
			if (rs.next()) {
				
				vo.setCodigo(rs.getInt(1));
				vo.setNombres(rs.getString(2));
				vo.setPaterno(rs.getString(3));
				vo.setMaterno(rs.getString(4));
				vo.setSexo(rs.getString(5));
				vo.setTipodocumento(rs.getString(6));
				vo.setNrodocumento(rs.getString(7));
				vo.setEmail(rs.getString(8));
				vo.setContrasena(rs.getString(9));
				vo.setFnacimiento(rs.getDate(10));
				vo.setCelular(rs.getString(11));
			
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public void eliminar(int idPersona) throws DAOExcepcion {
		System.out.println("PersonaDAO: eliminar(int idPersona)");
		String query = "DELETE FROM Persona WHERE idPersona=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idPersona);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public void actualizar(Persona vo) throws DAOExcepcion {
		System.out.println("PersonaDAO: actualizar(Persona vo)");
		String query = "update Persona set nombres = ?, paterno = ?, materno = ?, sexo = ? , tipodocumento = ? , nrodocumento = ? , email = ? , contrasena = ? , fnacimiento = ? , celular = ? where idPersona = ?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			
			stmt.setString(1, vo.getNombres());
			stmt.setString(2, vo.getPaterno());
			stmt.setString(3, vo.getMaterno());
			stmt.setString(4, vo.getSexo());
			stmt.setString(5, vo.getTipodocumento());
			stmt.setString(6, vo.getNrodocumento());
			stmt.setString(7, vo.getEmail());
			stmt.setString(8, vo.getContrasena());
			stmt.setDate(9, vo.getFnacimiento());
			stmt.setString(10, vo.getCelular());
			
						
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	
public Collection<Persona> listar() throws DAOExcepcion {
		System.out.println("PersonaDAO: listar()");
		Collection<Persona> c = new ArrayList<Persona>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "SELECT idPersona, nombre, paterno, materno, sexo, tipodocumento, nrodocumento, email, contrasena, fnacimiento, celular from Persona";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Persona vo = new Persona();
				
				vo.setCodigo(rs.getInt(1));
				vo.setNombres(rs.getString(2));
				vo.setPaterno(rs.getString(3));
				vo.setMaterno(rs.getString(4));
				vo.setSexo(rs.getString(5));
				vo.setTipodocumento(rs.getString(6));
				vo.setNrodocumento(rs.getString(7));
				vo.setEmail(rs.getString(8));
				vo.setContrasena(rs.getString(9));
				vo.setFnacimiento(rs.getDate(10));
				vo.setCelular(rs.getString(11));
			
				
				c.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

}


